package com.patrol.patrol.ui.appear;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.patrol.patrol.R;

import java.util.List;

public class ImageAdapter extends BaseAdapter {

    List<ImageInfo> images;
    Context context;
    OnItemDeleteClick onItemDeleteClick;
    OnItemClick onItemClick;

    public ImageAdapter(List<ImageInfo> images, Context context, OnItemDeleteClick onItemDeleteClick, OnItemClick onItemClick) {
        this.images = images;
        this.context = context;
        this.onItemDeleteClick = onItemDeleteClick;
        this.onItemClick = onItemClick;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public ImageInfo getItem(int position) {
        return images.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(
                    context).inflate(R.layout.item_image,null);
            holder=new ViewHolder();
            holder.imgDelete=convertView.findViewById(R.id.img_del);
            holder.imageView=convertView.findViewById(R.id.img_1);
            holder.imgPlay=convertView.findViewById(R.id.img_play);
            convertView.setTag(holder);
        }else {
            holder=(ViewHolder) convertView.getTag();
        }

        holder.imgPlay.setVisibility(View.GONE);

        Glide.with(context)
                .load(images.get(position).getImageUrl())
                .centerCrop()
                .placeholder(R.drawable.ic_zhanweitu)
                .crossFade()
                .into(holder.imageView);

        if(images.get(position).getImageType()==1){
            holder.imgPlay.setVisibility(View.VISIBLE);
        }else {
            holder.imgPlay.setVisibility(View.GONE);
        }

        holder.imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemDeleteClick.onItemDeleteClick(position);
            }
        });
        return convertView;
    }
    class ViewHolder{
        ImageView imageView;
        ImageView imgDelete;
        ImageView imgPlay;
    }

    interface OnItemDeleteClick{
        void onItemDeleteClick(int position);
    }

    interface OnItemClick{
        void onItemClick(int position);
    }

}
